using System;
using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Provider;
using Newtonsoft.Json.Linq;

namespace Sharp.Platform.Owin.OAuth.WeiBo.Provider
{
	public class WeiBoAuthenticatedContext : BaseContext
	{
		public string ProfileImageUrl
		{
			get;
			set;
		}

		public JObject User
		{
			get;
			private set;
		}

		public string AccessToken
		{
			get;
			private set;
		}

		public string RefreshToken
		{
			get;
			private set;
		}

		public TimeSpan? ExpiresIn
		{
			get;
			set;
		}

		public string Id
		{
			get;
			private set;
		}

		public string Name
		{
			get;
			private set;
		}

		public ClaimsIdentity Identity
		{
			get;
			set;
		}

		public AuthenticationProperties Properties
		{
			get;
			set;
		}

		public WeiBoAuthenticatedContext(IOwinContext context, JObject user, string accessToken, string refreshToken, string expires) : base(context)
		{
			if (user == null)
			{
				throw new ArgumentNullException(nameof(user));
			}
			this.User = user;
			this.AccessToken = accessToken;
			this.RefreshToken = refreshToken;
			this.Id = user["id"].ToString() ;
			this.Name =user["name"].ToString();
			this.ProfileImageUrl =user["profile_image_url"].ToString();
		}

		private static string PropertyValueIfExists(string property, IDictionary<string, JToken> dictionary)
		{
			if (!dictionary.ContainsKey(property))
			{
				return null;
			}
			return dictionary[property].ToString();
		}
	}
}
